'use strict'
const ipcRenderer = require('electron').ipcRenderer
const EventEmitter = require('events')
export default class RoomManage extends EventEmitter {
  constructor () {
    super()
    this.rooms = new Map()
    this.fns = {
      msg: function (msg) {
        let type = msg.type
        switch (type) {
          case 'msg':
            this.emit('chat', msg.data)
            break
          case 'data':
            let room = this.rooms.get(msg.key)
            if (room) {
              Object.assign(room, msg.data)
            }
            break
          case 'gift':
            this.emit('gift', msg)
            break
          default:

        }
      }.bind(this)
    }
    ipcRenderer.on('message', this.receiveMessage.bind(this))
  }
  receiveMessage (event, obj) {
    let type = obj.type
    if (type === 'connect') {
      this._postMessage({
        type: 'getRooms',
        data: {}
      })
    } else {
      let result = obj.data
      this.fns[type](result)
      switch (type) {
        case 'add':
          if (result.code === 0) {
            let key = this._getKey(result.data.platform, result.data.roomId)
            this.rooms.set(key, result.data)
          }
          break
        case 'getRooms':
          result.rooms.forEach((room) => {
            this.rooms.set(this._getKey(room.platform, room.roomId), room)
          })
          break
      }
    }
  }
  addRoom (platform, roomId, fn) {
    this._postMessage({
      type: 'add',
      data: {
        platform,
      roomId}
    })
    this.fns.add = fn
  }
  remove (platform, roomId) {
    let key = this._getKey(platform, roomId)
    this.rooms.delete(key)
    this._postMessage({
      type: 'remove',
      data: {
        platform,
      roomId}
    })
  }
  getRooms (fn) {
    this.fns.getRooms = fn
  }
  getRoom (platform, roomId) {
    let key = roomId ? this._getKey(platform, roomId) : platform
    return this.rooms.get(key)
  }
  getGift (key, fn) {
    this._postMessage({
      type: 'getGift',
      data: {
      key}
    })
    this.fns.getGift = fn
  }
  getRank (key, field, fn) {
    this._postMessage({
      type: 'getRank',
      data: {
        key,
      field}
    })
    this.fns.getRank = fn
  }
  getGiftCount (key, fn) {
    this._postMessage({
      type: 'getGiftCount',
      data: {
      key}
    })
    this.fns.getGiftCount = fn
  }
  hasRoom (platform, roomId) {
    return this.rooms.has(this._getKey(platform, roomId))
  }
  getOnLineChart (key, fn) {
    this._postMessage({
      type: 'getOnLineChart',
      data: {
      key}
    })
    this.fns.getOnLineChart = fn
  }
  getContrastChart (key, fn) {
    this._postMessage({
      type: 'getContrastChart',
      data: {
      key}
    })
    this.fns.getContrastChart = fn
  }
  getGiftChart (key, fn) {
    this._postMessage({
      type: 'getGiftChart',
      data: {
      key}
    })
    this.fns.getGiftChart = fn
  }
  getHistoryRoom (fn) {
    this._postMessage({
      type: 'getHistoryRoom',
      data: {}
    })
    this.fns.getHistoryRoom = fn
  }
  removeHistoryRoom (key) {
    this._postMessage({
      type: 'removeHistoryRoom',
      data: {
      key}
    })
  }
  winClose () {
    this._postMessage({
      type: 'winClose'
    })
  }
  winHide () {
    this._postMessage({
      type: 'winHide'
    })
  }
  _getKey (platform, roomId) {
    return platform.concat('_').concat(roomId)
  }
  _postMessage (msg) {
    ipcRenderer.send('message', msg)
  }
}
/* Installation */

RoomManage.installed = false

/**
 * Installation interface.
 * Install the necessary directives.
 */

RoomManage.install = function (Vue, options) {
  /* istanbul ignore if */
  if (RoomManage.installed) {
    return
  }
  Vue.prototype.$RoomManage = new RoomManage()
  RoomManage.installed = true
}
